blog

Home / DeveloperSection / Blogs / Action Filter in asp.net mvc

Action Filter in asp.net mvc

Manish Kumar3447 20-Jan-2017

The aim of this article is to explain the working of action filter. It is an attribute that can be applied in the controller. It tell that which the action execution is done. Action method in the asp.net mvc

Output cache- Output Cache cache the output for given interval of time

Handle error-Handle error action attribute handle the error when error occurred by controller. It redirect to another page.

Authorize-Authorize action provide the authority for particular user or user. It provide restriction.

There are four type of filter that support asp.net mvc

1-Authorization filter It implements IAuthorization attribute. It contain the logic for authentication for controller action.

2-Action Filter- It implements  IActionFilter attribute. It contain the logic that is executed before and after control action executes.

3-Result Filter-It implements IResult attribute. It contain the logic that is executed before and after the view result execution.

4-Exeception Filter-It implents IException attribute. It contain the logic for handle error it executes at the last of all filter.

These all type of filter is executed as it is written in serial .

If we want to change the order of execution of filter then we have to use Filter order property  

Let us take a example to execute               

Outputcache

using System;
using
System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Demo.Models;
 
 
namespace Demo.Controllers
{
    publicclassHomeController : BaseController
    {         
        [OutputCache (Duration=20)]
        publicActionResult Index()
        {
            ViewBag.Users =
Uow.EmpDetail.SelectAll();      
           
            return View();
        }
}

 

This attribute hold the output for 20 seconds.

HanddleError

using System;
using
System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Demo.Models;
 
 
namespace Demo.Controllers
{
    [HandleError]
    publicclassHomeController : BaseController
    {    
     
        [OutputCache (Duration=20)]
        publicActionResult Index()
        {
            ViewBag.Users =
Uow.EmpDetail.SelectAll();      
           
            return View();
        }}
}


Authorize 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Demo.Models;
 
 
namespace Demo.Controllers
{
    [HandleError]
    publicclassHomeController : BaseController
    {         
        [OutputCache (Duration=20)]
        [Authorize]
        publicActionResult Index()
        {
            ViewBag.Users =
Uow.EmpDetail.SelectAll();      
           
            return View();
        }
 
    [AcceptVerbs(HttpVerbs.Post)]
        publicActionResult Index(EmpDetail model)
        {
            if (ModelState.IsValid)
            {
                if (model.Id == 0)
                {
                   
Uow.EmpDetail.Insert(model);
 
                }
                else
                {
                    var data =
Uow.EmpDetail.SelectByID(model.Id);
                    if (data == null)
                       
Uow.EmpDetail.Insert(model);
                    else
                       
Uow.EmpDetail.Update(model);
                }
            }
                ViewBag.Users =
Uow.EmpDetail.SelectAll();
                return PartialView("UserList");          
         
        }    
        [HttpPost]
        [Authorize(Roles="Admin")]
        publicActionResult Delete(int Id)
        {
            Uow.EmpDetail.Delete(Id);         
            ViewBag.Users =
Uow.EmpDetail.SelectAll();           
            return PartialView("UserList");
        }    
        publicActionResult Edit(int Id)
        {
            var data = Uow.EmpDetail.SelectByID(Id);
            return Json(data, JsonRequestBehavior.AllowGet);
        }
      
 
    }
}
 


In the above example I have use authorize attribute in two place in index action and in delete action .If we try to access without log in then it shows error.

Understanding Action filter(c#)MVC

What are the different types of filters, in an asp.net mvc application

What is the use of action filters in an MVC application


Updated 17-Mar-2018

Leave Comment

Comments

Liked By